﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AStartTest
{
    public partial class MainForm : Form
    {
        private int _row;
        private int _col;

        private MapPos _startPos;
        private MapPos _endPos;
        private Map2D _map;

        public MainForm()
        {
            InitializeComponent();
        }

        protected override void OnShown(EventArgs e)
        {
            Reset();
            base.OnShown(e);
        }

        private void Reset()
        {
            panel1.Controls.Clear();
            _map = DATUtility.GetMap(100);

            for (int r = 0; r < _map.Row; r++)
            {
                for (int c = 0; c < _map.Col; c++)
                {
                    Button btn = new Button();

                    btn.Width = 16;
                    btn.Height = 16;
                    btn.Location = new Point(c * 16, r * 16);

                    MapCell cell = _map.GetCell(new MapPos(r, c));
                    if (cell.Value!=0)
                    {
                        btn.Tag = cell.MapPos;
                        btn.BackColor = Color.Pink;
                    }
                    else
                    {
                        btn.Tag = cell.MapPos;
                        btn.BackColor = Color.White;
                        btn.Click += btn_Click;
                    }
                    panel1.Controls.Add(btn);
                }
            }

            /*
            _map = new Map2D();
            _map.Row = 12;
            _map.Col = 12;
            _map.Cells = new MapCell[_map.Row*_map.Col];
            for (int r = 0; r < _map.Row; r++)
            {
                for (int c = 0; c < _map.Col; c++)
                {
                    Button btn = new Button();

                    btn.Width = 32;
                    btn.Height = 32;
                    btn.Location = new Point(c * 32, r * 32);

                    int v = new Random().Next(0, 10);
                    if (v == 2)
                    {
                        MapCell cell = new MapCell();
                        cell.Value = 1;
                        cell.MapPos = new MapPos(r, c);
                        _map.Cells[r * _map.Col + c] = cell;

                        btn.Tag = cell.MapPos;
                        btn.BackColor = Color.Pink;
                    }
                    else
                    {
                        MapCell cell = new MapCell();
                        cell.Value = 0;
                        cell.MapPos = new MapPos(r, c);
                        _map.Cells[r * _map.Col + c] = cell;

                        btn.Tag = cell.MapPos;
                        btn.BackColor = Color.White;
                        btn.Click += btn_Click;
                    }
                    panel1.Controls.Add(btn);

                    System.Threading.Thread.Sleep(20);
                }
            }
            */

            _startPos = null;
            _endPos = null;
        }

        private void UpdateUI()
        {
            if (_startPos != null)
            {
                textBox1.Text = string.Format("{0},{1}", _startPos.Row, _startPos.Col);
            }
            else
            {
                textBox1.Text = string.Empty;
            }

            if (_endPos != null)
            {
                textBox2.Text = string.Format("{0},{1}", _endPos.Row, _endPos.Col);
            }
            else
            {
                textBox2.Text = string.Empty;
            }
        }

        void btn_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            if (_startPos == null)
            {
                _startPos = btn.Tag as MapPos;
            }
            else if (_endPos == null)
            {
                _endPos = btn.Tag as MapPos;
            }

            UpdateUI();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Reset();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (_startPos == null || _endPos == null)
            {
                return;
            }

            //List<MapPos> pos = AStart.AA(_map, _startPos, _endPos);
            PointAS asStart = new PointAS();
            asStart.y = _startPos.Row;
            asStart.x = _startPos.Col;

            PointAS asEnd = new PointAS();
            asEnd.y = _endPos.Row;
            asEnd.x = _endPos.Col;
            List<MapPos> path = new AST().FindWay(asStart, asEnd, _map);

            if (path != null)
            {
                foreach (var item in panel1.Controls)
                {
                    Button btn = item as Button;
                    MapPos pos = btn.Tag as MapPos;

                    foreach (var pathNode in path)
                    {
                        if (pathNode.Row == pos.Row && pathNode.Col == pos.Col)
                        {
                            btn.BackColor = Color.Green;
                            break;
                        }
                    }
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            _startPos = null;
            _endPos = null;


            UpdateUI();
        }
    }
}
